<template>
  <div class="">
    <el-card>
      <!-- 顶部表单区域 -->
      <el-form :inline="true" class="form">
        <el-form-item label="用户名:">
          <el-input placeholder="请输入用户名"></el-input>
        </el-form-item>
        <!-- 表单右侧按钮区域 -->
        <el-form-item>
          <el-button type="success" size="default">搜索</el-button>
          <el-button type="warning" size="default">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 内容卡片区域 -->
    <el-card style="margin: 10px 0px">
      <!-- 内容顶部按钮 -->
      <el-button type="primary" size="default" @click="addUser">
        添加用户
      </el-button>
      <el-button type="danger" size="default">批量删除</el-button>
      <!-- 展示用户信息 -->
      <el-table style="margin: 10px 0px" border :data="userInfo">
        <!-- 用户信息复选框 -->
        <el-table-column
          type="selection"
          align="center"
          width="50px"
        ></el-table-column>
        <!-- 用户信息复选框 -->
        <el-table-column
          label="序号"
          align="center"
          width="70px"
          type="index"
        ></el-table-column>
        <!-- 用户信息ID -->
        <el-table-column
          label="ID"
          align="center"
          width="70px"
          prop="id"
        ></el-table-column>
        <!-- 用户信息名字 -->
        <el-table-column
          label="用户名字"
          align="center"
          prop="username"
          show-overflow-tooltip
        ></el-table-column>
        <!-- 用户信息名称 -->
        <el-table-column
          label="用户名称"
          align="center"
          prop="name"
          show-overflow-tooltip
        ></el-table-column>
        <!-- 用户信息职位 -->
        <el-table-column
          label="用户职位"
          align="center"
          prop="roleName"
          show-overflow-tooltip
        ></el-table-column>
        <!-- 用户信息创建时间 -->
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          show-overflow-tooltip
        ></el-table-column>
        <!-- 用户信息更新时间 -->
        <el-table-column
          label="更新时间"
          align="center"
          prop="updateTime"
          show-overflow-tooltip
        ></el-table-column>
        <!-- 用户信息操作 -->
        <el-table-column label="操作" align="center" width="300px">
          <template #="{ row, $index }">
            <!-- 用户信息按钮 -->
            <el-button type="primary" size="small" icon="">分配角色</el-button>
            <el-button
              type="warning"
              size="small"
              icon="Edit"
              @click="editUser(row)"
            >
              编辑
            </el-button>
            <el-button type="danger" size="small" icon="Delete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 底部分页器 -->
      <el-pagination
        v-model:current-page="pageNo"
        v-model:page-size="pageSize"
        :page-sizes="[5, 10, 25, 50]"
        :background="true"
        layout="prev, pager, next, jumper, ->, sizes, total"
        :total="total"
        @size-change="changeSize"
        @current-change="getUserInfo"
      />
      <!-- 用户详情抽屉区域 -->
      <el-drawer
        v-model="drawer"
        title="I am the title"
        :before-close="handleClose"
      >
        <span>Hi, there!</span>
      </el-drawer>
      <el-drawer v-model="drawer2" :direction="direction">
        <!-- 抽屉标题区域 -->
        <template #header>
          <h4>set title by slot</h4>
        </template>
        <template #default>
          <div>
            <el-radio v-model="radio1" label="Option 1" size="large">
              Option 1
            </el-radio>
            <el-radio v-model="radio1" label="Option 2" size="large">
              Option 2
            </el-radio>
          </div>
        </template>
        <template #footer>
          <div style="flex: auto">
            <el-button @click="cancelClick">cancel</el-button>
            <el-button type="primary" @click="confirmClick">confirm</el-button>
          </div>
        </template>
      </el-drawer>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
// 引入element-plus信息提示
import { ElMessage } from 'element-plus'
// 引入用户APi请求
import { reqUserInfo } from '@/api/acl/user/user'
// 引入用户信息ts类型
import type { userResponseData, Records, userData } from '@/api/acl/user/type'
// 分页器起始页数
let pageNo = ref<number>(1)
// 分页器展示的数据量
let pageSize = ref<number>(5)
// 分页器总页数
let total = ref<number>(0)
// 定义用户信息数组
let userInfo = ref<Records>([])
// 定义控制抽屉详情开关状态
let drawer = ref<boolean>(false)
// 页面加载时发请求获取用户信息
onMounted(() => {
  getUserInfo()
})
// 获取已有的用户信息
const getUserInfo = async (page: number = 1) => {
  // 收集当前页码
  pageNo.value = page
  const result: userResponseData = await reqUserInfo(
    pageNo.value,
    pageSize.value,
  )
  if (result.code == 200) {
    // 存储用户信息数据
    userInfo.value = result.data.records
    // 存储分页器总页数
    total.value = result.data.total
    // 删除成功的提示
    ElMessage({
      type: 'success',
      message: '获取用户信息成功',
    })
  } else {
    // 删除失败的提示
    ElMessage({
      type: 'error',
      message: '获取用户信息失败',
    })
  }
}
// 分页器下拉框发生改变时触发
const changeSize = () => {
  // 默认为第一页，一页几条数据通过 v-model:page-size="pageSize" 已经收集
  getUserInfo()
}
// 点击添加按钮
const addUser = () => {
  // 展示抽屉内容
  drawer.value = true
}
// 点击编辑按钮 row 即为已有的用户账号信息
const editUser = (row: userData) => {
  // 展示抽屉内容
  drawer.value = true
}
</script>
<style lang="scss" scoped>
.form {
  display: flex;
  justify-content: space-between;
}
</style>
